package models

import (
	"context"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
)

func GetMoodDescription(mood int) string {
	MoodList := map[int]string{1: "开心", 2: "快乐", 3: "普通", 4: "难过", 5: "生气"}
	return MoodList[mood]
}

type Trajectory struct {
	ID          primitive.ObjectID `json:"id" bson:"_id,omitempty"`
	Picture     []string           `json:"picture" bson:"picture" validate:"omitempty"`        // 图片路径
	Tag         []string           `json:"tag" bson:"tag" validate:"omitempty"`                // 标签
	Time        int                `json:"time" bson:"time" validate:"omitempty"`              // 时间
	Description string             `json:"description" bson:"description" validate:"required"` // 描述
	Video       []string           `json:"video" bson:"video" validate:"omitempty"`            // 视频路径
	Audio       []string           `json:"audio" bson:"audio" validate:"omitempty"`            // 音频路径
	User        string             `json:"user" bson:"user"`                                   // 用户ID
	Title       string             `json:"title" bson:"title" validate:"required"`             // 标题
	Location    string             `json:"location" bson:"location" validate:"omitempty"`      // 位置
	Weather     string             `json:"weather" bson:"weather" validate:"omitempty"`        // 天气
	Mood        string             `json:"mood" bson:"mood" validate:"omitempty"`              // 心情
	UserInfo    []User             `json:"userinfo" bson:"user_info" validate:"omitempty"`
}
type TrajectoryRepository struct {
	*BaseRepository[Trajectory]
}

func NewTrajectoryRepository(db *mongo.Database, collectionName string) *TrajectoryRepository {
	return &TrajectoryRepository{
		BaseRepository: NewBaseRepository[Trajectory](db, collectionName),
	}
}

// 连表查询示例
func GetTrajectoryWithUser(ctx context.Context, coll *mongo.Collection) ([]Trajectory, error) {
	// 聚合管道：$lookup关联User集合
	pipeline := mongo.Pipeline{
		{
			{Key: "$lookup", Value: bson.D{
				{Key: "from", Value: "users"},       // User集合名（需与数据库实际集合名一致）
				{Key: "localField", Value: "user"},  // Trajectory中存储用户ID的字段
				{Key: "foreignField", Value: "_id"}, // User集合的主键字段（_id）
				{Key: "as", Value: "user_info"},     // 结果存储字段名
			}},
		},
	}

	cursor, err := coll.Aggregate(ctx, pipeline)
	if err != nil {
		return nil, err
	}
	defer cursor.Close(ctx)

	var result []Trajectory
	if err = cursor.All(ctx, &result); err != nil {
		return nil, err
	}
	return result, nil
}

// func (r *TrajectoryRepository) Insert(picture string, tag string,description string) error {
// 	// person := Person{Name: name, Age: age, Hobby: hobby}
// 	// if err := verify.ValidateStruct(person); err != nil {
// 	// 	return err
// 	// }
// 	// _, err := r.collection.InsertOne(context.TODO(), bson.M{"name": name, "age": age, "hobby": hobby})
// 	return err
// }
